Checks whether a passed string is palindrome or not¶
Write a python function that checks whether a passed string is palindrome or not.
Note:
A palindrome is a word, phrase, or sequence that reads
the same backward as forward, e.g., madam or nurses run.
def is_palindrome(S):
left_pos = 0
right_pos = len(S) - 1
while right_pos >= left_pos:
if not S[left_pos] == S[right_pos]:
return False
left_pos += 1
right_pos -= 1
return True
# test
S = 'madam'
print(is_palindrome(S)) # True
as class¶
class Palindrome:
@staticmethod
def is_palindrome(word):
length = len(word)
for i in range(length // 2):
if word[i].lower() != word[length-i-1].lower():
return False
return True
# test
S = 'Deleveled'
print(Palindrome.is_palindrome(S)) # True
use all()¶
def is_palindrome_01(S):
return all(S[i].lower() == S[-i-1].lower() for i in range(len(S) // 2))
# test
S = 'Deleveled'
print(is_palindrome_01(S)) # True